﻿using Dapper.Contrib.Extensions;
using JuCheap.Store.DataService.Entity;
using Microsoft.Extensions.Configuration;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace JuCheap.Store.DataService
{
    public class BaseDataService<T> : IBaseDataService<T> where T : class, IBaseEntity
    {
        private readonly IConfiguration _configuration;

        public BaseDataService(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 单个添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<string> InsertAsync(T entity)
        {
            entity.Create();
            using (var connection = new SqlConnection(_configuration.GetConnectionString("DbConnection")))
            {
                await connection.InsertAsync(entity);
                return entity.Id;
            }
        }
    }
}
